﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using BBICMS.Model;
using System.Data;
using System.Data.Entity;
using BBICMS.Repository.Configuration;
using BBICMS.Infrastructure;
using BBICMS.Core;
using BBICMS.Model.Security;
using BBICMS.Model.Works;
using SIG.Model.Mapping;

namespace BBICMS.Repository
{
    public class DataContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Collection> Collections { get; set; }
        //public DbSet<QLayout> QLayouts { get; set; }
        //public DbSet<BLayout> BLayouts { get; set; }
        //public DbSet<BSize> BSizes { get; set; }
        //public DbSet<QSize> QSizes { get; set; }
        //public DbSet<Color> Colors { get; set; }
        //public DbSet<ColorLang> ColorLangs { get; set; }
        //public DbSet<Serie> Series { get; set; }
        //public DbSet<SerieLang> SerieLangs { get; set; }
        public DbSet<ProductCategory> ProductCategories { get; set; }
        public DbSet<ProductCategoryLang> ProductCategoryLangs { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ProductPhoto> ProductPhotos { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Menu> Menus { get; set; }
        public DbSet<Position> Positions { get; set; }
        public DbSet<Advertisement> Advertisements { get; set; }
        public DbSet<Carousel> Carousels { get; set; }
        public DbSet<Team> Teams { get; set; }

        public DbSet<Work> Works { get; set; }
        public DbSet<WorkType> WorkTypes { get; set; }
        public DbSet<Solution> Solutions { get; set; }
        public DbSet<Client> Clients { get; set; }
        public DbSet<Link> Links { get; set; }
        public DbSet<LinkCategory> LinkCategories { get; set; }

        public DbSet<Language> Languages { get; set; }

        public DbSet<Page> Pages { get; set; }
        public DbSet<PageLang> PageLangs { get; set; }

        public static string ConnectionStringName
        {
            get
            {
                if (ConfigurationManager.AppSettings["ConnectionStringName"]
                    != null)
                {
                    return ConfigurationManager.
                        AppSettings["ConnectionStringName"].ToString();
                }

                return "DefaultConnection";
            }
        }

        //static DataContext()
        //{
        //    Database.SetInitializer(new CustomDatabaseInitializer());
        //}

        public DataContext()
            : base(nameOrConnectionString: DataContext.ConnectionStringName)
        {
           // Configuration.LazyLoadingEnabled = false;
        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new PositionConfiguration());
            modelBuilder.Configurations.Add(new AdvertisementConfiguration());
            modelBuilder.Configurations.Add(new BlogConfiguration());
            modelBuilder.Configurations.Add(new PostConfiguration());

            //modelBuilder.Configurations.Add(new QSizeMap());
            //modelBuilder.Configurations.Add(new BSizeMap());
            //modelBuilder.Configurations.Add(new QLayoutMap());
            //modelBuilder.Configurations.Add(new BLayoutMap());
            //modelBuilder.Configurations.Add(new ColorMap());
            //modelBuilder.Configurations.Add(new ColorLangMap());
            //modelBuilder.Configurations.Add(new SerieMap());
            //modelBuilder.Configurations.Add(new SerieLangMap());
            modelBuilder.Configurations.Add(new ProductCategoryConfiguration());
            modelBuilder.Configurations.Add(new ProductCategoryLangMap());
            modelBuilder.Configurations.Add(new ProductMap());
            modelBuilder.Configurations.Add(new ProductPhotoMap());
            modelBuilder.Configurations.Add(new CategoryConfiguration());
            modelBuilder.Configurations.Add(new CollectionMap());

            modelBuilder.Configurations.Add(new UserMap());          
            modelBuilder.Configurations.Add(new RoleMap());
            modelBuilder.Configurations.Add(new MenuMap());

            modelBuilder.Configurations.Add(new PageMap());
            modelBuilder.Configurations.Add(new PageLangMap());

            modelBuilder.Configurations.Add(new CarouselConfiguration());
            modelBuilder.Configurations.Add(new TeamConfiguration());
            modelBuilder.Configurations.Add(new WorkConfiguration());
            modelBuilder.Configurations.Add(new WorkTypeConfiguration());
            modelBuilder.Configurations.Add(new SolutionConfiguration());

            modelBuilder.Configurations.Add(new LinkConfiguration());

            //base.OnModelCreating(modelBuilder);
        }


        private void ApplyRules()
        {

            foreach (var entry in this.ChangeTracker.Entries()
                        .Where(e => e.Entity is IAuditableEntity && (e.State == EntityState.Added) || (e.State == EntityState.Modified)))
            {
                IAuditableEntity e = entry.Entity as IAuditableEntity;
                if (e != null)
                {
                    DateTime now = DateTime.Now;
                    if (entry.State == EntityState.Added)
                    {
                        e.AddedBy = Helpers.CurrentUserName;
                        e.AddedDate = now;
                    }


                    e.UpdatedBy = Helpers.CurrentUserName;
                    e.UpdatedDate = now;
                }
            }
        }
        public override int SaveChanges()
        {
            this.ApplyRules();
            return base.SaveChanges();
        }

    }
}
